feat: make it so you can optionally have DiscardUnknown not apply to enums #46
+73
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
70db1e1#diff-4bfd19b0e2b567c0f35f87a774a43846bc5b549661d952fb1177867cd762e6ba made it so
DiscardUnknown
also applied to enum values following the discussion golang/protobuf#1208 to comply with the C++ behavior.The comment by golang/protobuf#1208 (comment) directly applies to my situation in that for historical reasons we had
AllowUnknownFields
set to true in the oldjsonpb
code. This did not affect ENUMS and allowed our service to properly throw a 400 error for invalid ENUM values (yes I know we do not follow best practice of having the zero value be invalid). In the migration to this code we still needDiscardUnknown
to be true, but also retain the behavior of validating enum fields.To achieve this I added
ErrorOnUnknownEnumValue
toUnmarshalOptions
that will still letDiscardUnknown
apply to field names, but NOT ENUM values. This change will have no affect ifDiscardUnknown
is set to false and no affect on any other field type except enums. I also made the default value ofErrorOnUnknownEnumValue
retain current behavior ifDiscardUnknown
is set to true.